﻿using System;
using Mbs.ComponentModel.Container.Activation;
using Mbs.ComponentModel.Container.Context;
using Mbs.ComponentModel.Container.Metadata;

namespace Mbs.ComponentModel.Container.Lifestyle
{
    [Serializable]
    public class LifestyleManagerAdapter:MarshalAndDisposableObject,ILifestyleManager
    {
        public IMicroKernel Kernel { get; private set; }
        public IActivator Activator { get; private set; }
        public IComponentInfo Info { get; private set; }
        public Action<IComponentInfo, object> OnDestroyed { get; private set; }

        public virtual void Init(IActivator activator, IMicroKernel kernel, IComponentInfo info, Action<IComponentInfo, object> onDestroyed)
        {
            this.Activator = activator;
            this.Kernel = kernel;
            this.Info = info;
            OnDestroyed = onDestroyed;
        }

        public virtual object Get(ICreateContext ctx)
        {
            return Activator.Create(ctx);
        }

        public virtual bool Release(object instance)
        {
            return true;
        }

        public virtual bool Release()
        {
            return true;
        }

        protected override void Dispose(bool disposing)
        {
            Release();
            OnDestroyed = null;
        }
    }
}
